import configureStore from 'redux-mock-store'

import createHandlersMiddleware from 'middlewares/createHandlersMiddleware'
import 'libs/promise.finally'

const setup = (props = {}) => {
  const middlewares = [createHandlersMiddleware(props.handlers)]
  const mockStore = configureStore(middlewares)
  const store = mockStore({})

  return {
    store,
  }
}

it('call `beforeHandler` && `afterHandler`', () => {
  const type = 'FETCH_LIST_SUCCESS'
  const beforeHandler = jest.fn()
  const afterHandler = jest.fn()
  const { store } = setup({
    handlers: [
      {
        type,
        beforeHandler,
        afterHandler,
      },
    ],
  })

  store.dispatch({ type })

  expect(beforeHandler).toBeCalled()
  expect(afterHandler).toBeCalled()
})

it('ignore `beforeHandler` && `afterHandler`', () => {
  const type = 'FETCH_LIST_SUCCESS'
  const { store } = setup({
    handlers: [
      {
        type,
      },
    ],
  })

  store.dispatch({ type })
  expect(store.getActions()).toMatchSnapshot()
})

it("don' pass `handlers`", () => {
  const type = 'FETCH_LIST_SUCCESS'
  const { store } = setup()

  store.dispatch({ type, meta: {} })

  expect(store.getActions()).toMatchSnapshot()
})
